package org.bbelovic.devel.exercises;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Rounding {

    public static float round(final float input) {
        final String fInput = Float.toString(input);
        BigDecimal bd = new BigDecimal(fInput);
        bd = bd.setScale(4, RoundingMode.HALF_UP);
        return bd.floatValue();
    }
    
    public static float scale(final float input) {
        BigDecimal bd = new BigDecimal(Float.toString(input)).setScale(5, RoundingMode.HALF_UP);
        System.out.println("input -> " + input);
        System.out.println("bd.toString() -> "+ bd.toString());
        System.out.println("bd.floatValue() -> " + bd.floatValue());
        return bd.floatValue();
    }
    
    
    public static void main(String[] args) {
//        final float f1 = unsafeAddition();
//        float result = round(f1);
//        System.out.printf("%4f", result);
//        System.out.println(result);
        unsafeAddition();
        float f1 = scale(1.0001f);
        float f2 = scale(1.0000f);
    }
    
    public static int compare(final float first, final float second, final int scale) {
        final BigDecimal firstBd = new BigDecimal(Float.toString(first)).setScale(scale, RoundingMode.HALF_UP);
        final BigDecimal secondBd = new BigDecimal(Float.toString(second)).setScale(scale, RoundingMode.HALF_UP);
        System.out
                .println("firstBd=" + firstBd + ", secondBd=" + secondBd);
        return firstBd.compareTo(secondBd);
    }
    
    
    private static float unsafeAddition() {
        float f = 0.0F;
        final float increment = 0.1f;
        for (int i = 0; i < 10; i++) {
            f = f + increment;
        }
        System.out.println(f + " "+ 0.1f);
        return f;
    }
}
